﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.PhotoRindGrid;
using MINERP.BLL.InnerReturn;
using MINERP.MODEL.FoundationModel;


namespace MINERP.PhotoRindGrid.Forms
{
    public partial class FrmFanDan : Form
    {
        public FrmFanDan(List<MINERP.BLL.InnerReturn.PhotoView> inList, int inUserId, int inCurrTech, int inGridId)
        {
            InitializeComponent();
            listPhotoView = inList;
            UserId = inUserId;
            CurrTechId = inCurrTech;
            GridId = inGridId;
        }
        private List<MINERP.BLL.InnerReturn.PhotoView> listPhotoView;
        private int UserId;
        private int CurrTechId;
        private int GridId;
        private int ProductId;
        private int ZeRenID=0;
        private int ZeRenTechId = 0;
        private InnerReturnBLL _bll = new InnerReturnBLL();
        private void FrmFanDan_Load(object sender, EventArgs e)
        {
            ProductId = _bll.GetProductIDByGridId(GridId);
            this.textEditFaQiRen.Text = _bll.GetUser(UserId).UserName;
            comboBoxErrFlow.DisplayMember = "TechName";
            comboBoxErrFlow.ValueMember = "TechId";
            comboBoxErrFlow.DataSource = _bll.GetReturnList(CurrTechId, ProductId);
            BangDingTech(_bll.GetReturnList(CurrTechId,ProductId));

        }

        private void comboBoxErrFlow_SelectionChangeCommitted(object sender, EventArgs e)
        {

            int techId = ((Tb_Tech_Info)comboBoxErrFlow.SelectedItem).TechId;
            ZeRenTechId = techId;
            User_Info info = _bll.GetUser(ProductId, techId);
            textEditZeRenRen.Text = info.UserName;
            ZeRenID = info.UserID;
        }
        public void BangDingTech(List<Tb_Tech_Info> infos)
        {
            //comboBoxErrFlow.DisplayMember = "TechName";
            //comboBoxErrFlow.ValueMember = "TechId";
            //comboBoxErrFlow.DataSource = infos;
         
            if (infos == null)
            {
                return;
            }
            int intTear = infos.Count;
            if (intTear <= 2)
            {
                simpleButtonOK.Enabled = false;
            }
            else
            {
                simpleButtonOK.Enabled = true;
            }
            if (flowLayoutPanelFlow.Controls.Count == 0)
            {
                for (int i = 0; i < intTear; i++)
                {
                    System.Windows.Forms.Label leable = new Label();
                    System.Windows.Forms.CheckBox checkBox = new CheckBox();
                    checkBox.Text = infos[i].TechName;
                    checkBox.Tag = infos[i];
                    checkBox.AutoSize = true;
                    checkBox.Checked = true;
                    //checkBox.Enabled = false;
                    leable.Text = "->";
                    checkBox.Anchor = AnchorStyles.Left;
                    flowLayoutPanelFlow.Controls.Add(checkBox);
                    leable.Anchor = AnchorStyles.Left;
                    if (infos[i].TechName.Equals("打印管理") || infos[i].TechName.Equals("输出管理"))
                    {
                        checkBox.Enabled = false;
                    }
                    if (infos[i].TechName.Equals("调色"))
                    {
                        checkBox.Checked = false;
                    }
                    leable.AutoSize = true;
                    if (i < intTear - 1)
                    {
                        flowLayoutPanelFlow.Controls.Add(leable);
                    }
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                var arrCon = flowLayoutPanelFlow.Controls;
                List<Tb_Tech_Info> techInfos = new List<Tb_Tech_Info>();
                foreach (Control con in arrCon)
                {
                    if (con.Tag != null && con.Tag.GetType().Name.Equals("Tb_Tech_Info"))
                    {
                        CheckBox CB = ((CheckBox)(con));
                        if (CB.Checked == true)
                        {
                            techInfos.Add((Tb_Tech_Info)CB.Tag);
                        }
                    }
                }
                if (ZeRenID == 0)
                {
                    MessageBox.Show("请选择责任流程。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return;
                }
                if (ZeRenTechId == 0)
                {
                    MessageBox.Show("请选择责任流程。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return;
                }
                _bll.CreateReturnOrder(ProductId, techInfos, listPhotoView, UserId, ZeRenID, this.richTextBoxRemark.Text, ZeRenTechId, UserId);
                MessageBox.Show("返工单创建成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

    }

}

